//
//  BJLoanBusiness.m
//  SZBank
//
//  Created by rimi on 16/7/26.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJLoanBusiness.h"

@interface BJLoanBusiness ()<ButtonAndTableViewDelegate>
{
    CGFloat frameWidth;/**< 自身宽 */
    CGFloat frameHeight;/**< 自身高 */
    NSMutableArray *secondButtonTableArray;/**< 复选项选项数组 */
    NSArray *orginDataArray;/**< 最初数组 */
    NSMutableDictionary *dictionary_secondData;/**< 复选数据源 */

}

@property (nonatomic)ButtonAndTableView *firstButtonTable;/**< 主选项 */
@property (nonatomic)ButtonAndTableView *secondButtonTable;/**< 副选项 */
@property (nonatomic)UIScrollView *scrollView;/**< 滚动视图 */
@end

@implementation BJLoanBusiness

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //打开可交互属性
        self.userInteractionEnabled = YES;
        //赋值背景图
        self.image = [LocalDataManager getImageWithSourceOfPath:@"储蓄存款背景"];
        //赋值自身宽高
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        //初始化数据数组
        secondButtonTableArray = [NSMutableArray array];
        orginDataArray = [NSArray array];
        dictionary_secondData = [NSMutableDictionary dictionary];
        //赋给初始值
        NSArray *array = @[@"个人一手住房贷款",@"个人二手住房贷款",@"个人住房公积金（组合）贷款"];
        [secondButtonTableArray addObjectsFromArray:array];
        //获取网络数据

        [NetWorkingDataManager getProtocolTemplateUpdateWithProtocolid:@"402880c43fe174f9013fe1873e780008" success:^(id sender) {
            orginDataArray = sender;
            NSMutableArray *dataArray_second = [NSMutableArray array];
            for (int i = 0; i <((NSArray *)sender).count; i ++) {
                NSDictionary *dict = ((NSArray *)sender)[i];
                if ([((NSArray *)dict[@"protocolData"][@"个贷类别"]).firstObject isEqualToString:@"住房类贷款"]) {
                    [dictionary_secondData setObject:dict forKey:((NSArray *)dict[@"protocolData"][@"贷款名称"]).firstObject];
                }
                
                if ([dict[@"id"] isEqualToString:@"402886c240b9db190140c8d745ce018f"]) {
                    [dataArray_second addObject:dict[@"protocolData"][@"个贷类别"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"贷款名称"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"产品简介"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"贷款对象"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"贷款条件"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"贷款额度"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"贷款期限"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"贷款利率"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"担保方式"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"申请资料"][0]];
                    [dataArray_second addObject:dict[@"protocolData"][@"还款方式"][0]];
                }
            }
            
            [self creatTableToScrollView];
            [self addMutabelLabelToScrollViewWithDataArray:dataArray_second];
        } failure:^(id sender) {
            
        }];
        
        //添加视图
        [self addSubview:self.firstButtonTable];
        [self addSubview:self.secondButtonTable];
        [self addSubview:self.scrollView];

        
    }
    return self;
}

#pragma mark -- ButtonAndTableViewDelegate
- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
  
    if (btnTableView.tag == 101) {
       
        //移除副选框所有内容
        [secondButtonTableArray removeAllObjects];
        [dictionary_secondData removeAllObjects];

        
        
            NSMutableArray *array_id = [NSMutableArray array];
        
        
        
            for (NSDictionary *dictionary in orginDataArray) {
                if ([selectTitle isEqualToString: ((NSArray *)dictionary[@"protocolData"][@"个贷类别"]).firstObject]) {
                    if (![((NSString *)((NSArray *)dictionary[@"protocolData"][@"贷款名称"])[0]) isEqualToString:@"12"] && ![((NSString *)((NSArray *)dictionary[@"protocolData"][@"贷款名称"])[0]) isEqualToString:@"111"]) {
                      [array_id addObject:dictionary[@"id"]];
                    }
                }
            }
        
         for (NSString *string_id in array_id) {
               for (NSDictionary *dictionary in orginDataArray) {
                if ([string_id isEqualToString:dictionary[@"id"]]) {
                    if (![((NSString *)((NSArray *)dictionary[@"protocolData"][@"贷款名称"])[0]) isEqualToString:@"12"] && ![((NSString *)((NSArray *)dictionary[@"protocolData"][@"贷款名称"])[0]) isEqualToString:@"111"]) {
                        [dictionary_secondData setObject:dictionary forKey:((NSArray *)dictionary[@"protocolData"][@"贷款名称"]).firstObject];
                        [secondButtonTableArray addObject:((NSArray *)dictionary[@"protocolData"][@"贷款名称"]).firstObject];
                    }
                    
                }
            }
            
        }
        //重新加载复选框
         [self.secondButtonTable removeFromSuperview];
        
        _secondButtonTable = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(frameWidth * 0.45, frameHeight * 0.02, frameWidth * 0.5, frameHeight * 0.05) dataSource:@{@"title":secondButtonTableArray[0],@"list":secondButtonTableArray} backImageName:@"储蓄存款_01" selectImageName:@"储蓄存款_01"];
        _secondButtonTable.delegate = self;
        [self addSubview:self.secondButtonTable];
        
        
        
        
        //更换table显示的内容
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        //调用方法
        [self creatTableToScrollView];
        
        NSMutableArray *dataArray_second = [NSMutableArray array];
        [dictionary_secondData enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if ([obj[@"id"] isEqualToString:array_id[0]]) {
               
                [dataArray_second addObject:obj[@"protocolData"][@"个贷类别"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"贷款名称"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"产品简介"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"贷款对象"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"贷款条件"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"贷款额度"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"贷款期限"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"贷款利率"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"担保方式"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"申请资料"][0]];
                [dataArray_second addObject:obj[@"protocolData"][@"还款方式"][0]];
                
            }
           
        }];
        [self addMutabelLabelToScrollViewWithDataArray:dataArray_second];
        
    }else{
        if (dictionary_secondData.count != 0 ) {
            //更换table显示的内容
            [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            NSMutableArray *dataArray_second = [NSMutableArray array];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"个贷类别"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"贷款名称"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"产品简介"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"贷款对象"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"贷款条件"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"贷款额度"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"贷款期限"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"贷款利率"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"担保方式"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"申请资料"][0]];
            [dataArray_second addObject:dictionary_secondData[selectTitle][@"protocolData"][@"还款方式"][0]];
            [self creatTableToScrollView];
            [self addMutabelLabelToScrollViewWithDataArray:dataArray_second];
        }
      
    }

}

#pragma mark -- actions
/**
 *  添加固定线条label
 */
- (void)creatTableToScrollView{
    CGFloat scrWidth = self.scrollView.frame.size.width;
    CGFloat scrHight = self.scrollView.frame.size.height;
    //横线
    for (int i = 0; i < 15; i ++) {
            UIView *hLine = [[UIView alloc]initWithFrame:CGRectMake(scrWidth * 0.05,scrHight * 0.04 + scrHight * 0.25 * i, scrWidth * 0.9, scrWidth * 0.002)];
        hLine.backgroundColor = [UIColor lightGrayColor];
        if (i == 5 || i == 12 || i == 10) {
            hLine.hidden = YES;
        }
        [self.scrollView addSubview:hLine];
    }

    //竖线
    for (int i = 0; i < 3; i ++) {
        UIView *vLine = [[UIView alloc]init];
        if (i == 2) {
            vLine.frame = CGRectMake(scrWidth * 0.95, scrHight * 0.04, scrWidth * 0.002, scrHight * 3.5);
        }else{
            vLine.frame = CGRectMake(scrWidth * 0.05 + scrWidth * 0.15 * i, scrHight * 0.04, scrWidth * 0.002, scrHight * 3.5);
        }
        vLine.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:vLine];
    }
    //表头label
    NSArray *tableTitle = @[@"个贷类别",@"贷款名称",@"产品简介",@"贷款对象",@"贷款条件",@"贷款额度",@"贷款期限",@"贷款利率",@"担保方式",@"申请资料",@"还款方式"];
    for (int i = 0; i <  11; i ++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = tableTitle[i];
        label.font = [UIFont systemFontOfSize:FONT_SIZE];
        label.textAlignment = NSTextAlignmentCenter;
        if (i == 4) {
             label.frame = CGRectMake(scrWidth * 0.06, scrHight * 0.25 + scrHight * 0.25 * i , scrWidth * 0.14, scrHight * 0.08);
        }else if (i < 4){
            label.frame = CGRectMake(scrWidth * 0.06, scrHight * 0.135 + scrHight * 0.25 * i , scrWidth * 0.14, scrHight * 0.08);
        }else if (i > 4 && i < 8){
            label.frame = CGRectMake(scrWidth * 0.06, scrHight * 0.38 + scrHight * 0.25 * i , scrWidth * 0.14, scrHight * 0.08);
        }else if (i > 7 && i < 10){
            label.frame = CGRectMake(scrWidth * 0.06, scrHight * 0.5 + scrHight * 0.25 * i + (i - 8) * scrHight * 0.26, scrWidth * 0.14 , scrHight * 0.08);
        }else{
             label.frame = CGRectMake(scrWidth * 0.06, scrHight * 0.88 + scrHight * 0.25 * i , scrWidth * 0.14, scrHight * 0.08);
        }
        

        
        [self.scrollView addSubview:label];
    }
    
    
}

- (void)addMutabelLabelToScrollViewWithDataArray:(NSArray *)dataArray{
    CGFloat scrWidth = self.scrollView.frame.size.width;
    CGFloat scrHight = self.scrollView.frame.size.height;
    
    for (int i = 0 ; i < 11; i ++) {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:FONT_SIZE];
        label.numberOfLines = 0;
        label.text = dataArray[i];
        if (i < 2) {
            label.textAlignment = NSTextAlignmentCenter;
        }
        
        if (i < 4) {
            label.frame = CGRectMake(scrWidth * 0.225, scrHight * 0.04 + scrHight * 0.25 * i, scrWidth * 0.7, scrHight * 0.25);
        }else if (i == 4){
             label.frame = CGRectMake(scrWidth * 0.225, scrHight * 0.04 + scrHight * 0.25 * i, scrWidth * 0.7, scrHight * 0.5);
        }else if (i > 4 && i < 8){
            label.frame = CGRectMake(scrWidth * 0.225, scrHight * 0.29 + scrHight * 0.25 * i, scrWidth * 0.7, scrHight * 0.25);
        }else if (i > 7 && i < 10){
            label.frame = CGRectMake(scrWidth * 0.225, scrHight * 0.29 + scrHight * 0.25 * i + scrHight * (i - 8) * 0.25, scrWidth * 0.7, scrHight * 0.5);
        }else{
            label.frame = CGRectMake(scrWidth * 0.225, scrHight * 0.79 + scrHight * 0.25 * i, scrWidth * 0.7, scrHight * 0.25);
        }
        
        
               [self.scrollView addSubview:label];
    }
}

#pragma  mark -- getters
- (ButtonAndTableView *)firstButtonTable{
    if (!_firstButtonTable) {
        _firstButtonTable = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(frameWidth * 0.04, frameHeight * 0.02, frameWidth * 0.3, frameHeight * 0.05) dataSource:@{@"title":@"住房类贷款",@"list":@[@"住房类贷款",@"经营类贷款",@"消费类贷款"]} backImageName:@"储蓄存款_01" selectImageName:@"储蓄存款_01"];
        _firstButtonTable.delegate = self;
        _firstButtonTable.tag = 101;
    }
    return _firstButtonTable;
}


- (ButtonAndTableView *)secondButtonTable{
    if (!_secondButtonTable) {
        _secondButtonTable = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(frameWidth * 0.45, frameHeight * 0.02, frameWidth * 0.5, frameHeight * 0.05) dataSource:@{@"title":secondButtonTableArray[0],@"list":secondButtonTableArray} backImageName:@"储蓄存款_01" selectImageName:@"储蓄存款_01"];
        _secondButtonTable.delegate = self;
        _secondButtonTable.tag = 102;
    }
    return _secondButtonTable;
}


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.secondButtonTable.frame), frameWidth, frameHeight - self.secondButtonTable.frame.size.height)];
        _scrollView.contentSize = CGSizeMake(frameWidth, frameHeight * 4);
        _scrollView.showsVerticalScrollIndicator = NO;
    }
    return _scrollView;
}
@end
